﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace TELOS_Konverter
{
    class ErrorHandler
    {
        static private List<Error> errors = new List<Error> { };
        private MessageHandler mh = new MessageHandler();
        
        public void createError(Exception e, string msg)
        {
            errors.Add(new Error(e, msg));
        }

        public void createError(Exception e)
        {
            errors.Add(new Error(e));
        }

        public void createError(string msg)
        {
            errors.Add(new Error(msg));
        }

        public void clearErrors()
        {
            errors.Clear();
        }

        public void showErrors()
        {
            for (int i = 0; i < errors.Count; i++)
                mh.alert(errors[i].getException().ToString() + "\n" + errors[i].getMessage());
        }

        // --------------------- NOT TESTED -----------------------------------
        public void writeErrorsToFile()
        {
            List<string> content = new List<string>{};
            for (int i = 0; i < errors.Count; i++) {
                string e = "";
                string msg = "";
                if (errors[i].getException() != null)
                    e = errors[i].getException().ToString();
                if (errors[i].getMessage() != "" && errors[i].getException() != null)
                    msg = ": "+errors[i].getMessage();

                content.Add(e+msg);
            }

            string now = DateTime.Now.ToShortDateString() + DateTime.Now.ToShortTimeString();
            now = "";
            File.WriteAllLines("errorLog"+now+".txt", content);
        }
    }
}
